﻿using EtcpSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace EtcpSharpTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var tcp = ETcpFactoryService.Create())
            {
                tcp.Log = Console.WriteLine;
                tcp.Host = "0.0.0.0";
                tcp.Port = 10000;
                tcp.Connected += Tcp_Connected;
                tcp.Disconnected += Tcp_Disconnected;
                tcp.Received += Tcp_Received;
                var str = tcp.Initialize();
                tcp.Start();
                Console.WriteLine("服务器启动成功");
                Console.ReadKey();
            }

            Console.ReadKey();
        }

        /// <summary>
        /// 收到数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Tcp_Received(object sender, ETCPClientDataReceivedEventArgs e)
        {
            int id = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine("线程ID：" + id + "|" + e.Message);
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Tcp_Disconnected(object sender, ETCPClientDisconnectedEventArgs e)
        {
            Console.WriteLine("客户端离开：" + e.ClientId);
        }

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Tcp_Connected(object sender, ETCPClientConnectedEventArgs e)
        {
            Console.WriteLine("收到客户端连接：" + e.Client.Id);
        }
    }
}